package logic

import (
	"context"
	"person/personrpc/personrpcclient"
	"person/pkg"
	"strconv"

	"person/api/internal/svc"
	"person/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserPerfectLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserPerfectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserPerfectLogic {
	return &UserPerfectLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserPerfectLogic) UserPerfect(req *types.UserPerfectRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	perfect, err := l.svcCtx.PersonRpc.UserPerfect(l.ctx, &personrpcclient.UserPerfectRequest{
		Name:     req.Name,
		Sex:      req.Sex,
		Account:  req.Account,
		Password: req.Password,
	})
	if err != nil {
		return &types.Response{
			Message: err.Error(),
			Data:    nil,
			Code:    500,
		}, nil
	}

	userId := strconv.Itoa(int(perfect.Id))
	handler, err := pkg.TokenHandler(userId)
	return &types.Response{
		Message: "完善成功",
		Data:    handler,
		Code:    200,
	}, nil
}
